package com.epoch.servicecommunication.service.impl;

import bean.vo.CardPhoto;
import com.epoch.servicecommunication.bean.Card;
import com.epoch.servicecommunication.dao.CardDao;
import com.epoch.servicecommunication.dao.ConsumerDao;
import com.epoch.servicecommunication.service.CardService;
import com.epoch.servicecommunication.service.CommunicationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import utils.AssertUtil;
import utils.FileType;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author mi
 */
@Service
@Slf4j
public class CardServiceImpl implements CardService {
    @Resource
    private CardDao cardDao;

    @Autowired
    private ConsumerDao consumerDao;

    @Autowired
    private CommunicationService communicationService;
    /**
     * 添加用户发言
     * @param card
     */
    @Override
    public void saveCard(Card card){
        //非空校验，判断用户编号是否为空
        AssertUtil.isTrue(StringUtils.isBlank(card.getCsId()),"用户编号不能为空！");
        //添加用户打卡，判断受影响的行数
        AssertUtil.isTrue(cardDao.saveCard(card)<1,"用户发言添加失败！");
    }

    /**
     * 删除用户发言
     * @param id
     */
    @Override
    public void deleteCard(Integer id){
        //非空校验，判断用户编号是否为空
        AssertUtil.isTrue(id==null,"发布编号不能为空！");
        //删除用户打卡，判断受影响的行数
        AssertUtil.isTrue(cardDao.deleteCard(id)<1,"发言删除失败！");
    }


    /**
     * 朋友圈查询发言
     * @return
     */
    @Override
    public List<Object> getCard() {
        List <Card> cards=cardDao.getCard();
        List <Object> resultList = new ArrayList<>();
        for (Card list:cards){
            HashMap<String,Object> cardMap = new HashMap<>(10);
            cardMap.put("id",list.getId());
            cardMap.put("csId",list.getCsId());
            cardMap.put("csName",consumerDao.getById(list.getCsId()).getCsName());
            cardMap.put("content",list.getContent());
            cardMap.put("cardName",list.getCardName());
            cardMap.put("time",list.getTime());
            cardMap.put("images",getImage(list.getId()));
            List<Object> obj=communicationService.getCommunication(list.getId().toString());
            cardMap.put("communication",obj);
            resultList.add(cardMap);
        }
        return  resultList;
    }


    @Override
    public String addPhoto(MultipartFile multipartFile, Integer cardId) {
        //文件存在且不为空
        if (multipartFile != null && !multipartFile.isEmpty()){
            InputStream inputStream;
            try{
                inputStream = multipartFile.getInputStream();
                String fileType = FileType.getFileType(inputStream);
                if (Objects.equals(fileType, "png") || Objects.equals(fileType, "jpg")){
                    //过滤文件
                    //将文件保存到数据库
                    cardDao.addPhoto(cardId, multipartFile.getBytes(), fileType);
                    return getImage(cardId,cardDao.getMaxPhotoId());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public Integer getMaxId() {
        if (cardDao.getMaxCardId() != null){
            return cardDao.getMaxCardId();
        }
        return 1;
    }

    /**
     * 获取图片
     */
    @Override
    public List<String> getImage(Integer carId){
        List <CardPhoto> cp=cardDao.getPhotoById(carId);
        List <String> resultList = new ArrayList<>();
        for(CardPhoto list:cp){
            byte[] byteImage = list.getPhoto();
            String photoType = list.getPhotoType();
            if(photoType!=null){
                String base="data:image/"+photoType+";base64," + Base64.getEncoder().encodeToString(byteImage);
                resultList.add(base);
            }
        }
        return resultList;
    }

    @Override
    public String getImage(Integer carId,Integer photoId) {
        List <CardPhoto> cp=cardDao.getPhotoById(carId);
        for(CardPhoto list:cp){
            if(list.getId().equals(photoId)){
                byte[] byteImage = list.getPhoto();
                String photoType = list.getPhotoType();
                if(photoType!=null){
                    return "data:image/"+photoType+";base64," + Base64.getEncoder().encodeToString(byteImage);
                }
            }
        }
        return null;
    }
}
